package org.lex.dbq;

import java.sql.Connection;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.lex.dbq.view.Actions;
import org.lex.dbq.view.action.AddDriverAction;
import org.lex.dbq.view.action.AddTemplateAction;
import org.lex.dbq.view.action.ConnectSessionAction;
import org.lex.dbq.view.action.ConnectTemplateAction;
import org.lex.dbq.view.action.ViewProfileAction;
import org.lex.dbq.view.action.ViewSchemaAction;
import org.lex.dbq.view.action.ViewTypeAction;
import org.lex.dbq.view.action.ViewVersionAction;
import org.lex.dbq.view.browser.DriverPane;
import org.lex.dbq.view.browser.SessionPane;
import org.lex.dbq.view.browser.TemplatePane;
import org.lex.dbq.view.sql.SqlPane;
import org.lex.swt.SWTUtils;
import org.lex.swt.app.ActionMap;
import org.lex.swt.app.Application;
import org.lex.swt.size.WindowUtils;

public class DBQApplication extends Application {
	private static final DBQApplication INSTANCE = new DBQApplication();

	public static void main(String[] args) {
		DBQApplication.getApplication().launch(args);
	}

	public static DBQApplication getApplication() {
		return INSTANCE;
	}

	/**
	 * Start and run the application
	 */
	public void launch(String[] args) {
		System.out.println("app home: " + getContext().getLocalStorage().getApplicationHome());
		INSTANCE.open();
		Display.getCurrent().dispose();
	}

	public DBQOperator getOperator() {
		return operator;
	}

	public void addConnection(String title, final Connection conn) {
		TabItem item = new TabItem(sqlTabFolder, SWT.NONE);
		item.setText(title);
		SqlPane pane = new SqlPane(sqlTabFolder);
		item.setControl(pane);
		pane.setConnection(conn);
	}

	@Override
	public String getApplicationName() {
		return "dbq";
	}

	private DBQOperator operator = null;
	TabFolder sqlTabFolder = null;

	private DBQApplication() {
		super(null);
		this.setBlockOnOpen(true);
		this.addMenuBar();
		this.operator = new DBQOperator();
	}

	private void initActionMap() {
		ActionMap root = getContext().getActionMap();
		root.put(Actions.OPEN_TEMPLATE, new ConnectTemplateAction());
		root.put(Actions.ADD_DRIVER, new AddDriverAction());
		root.put(Actions.ADD_TEMPLATE, new AddTemplateAction());
		root.put(Actions.OPEN_SESSION, new ConnectSessionAction());
		root.put(Actions.SHOW_PROFILE, new ViewProfileAction());
		root.put(Actions.SHOW_SCHEMA, new ViewSchemaAction());
		root.put(Actions.SHOW_VERSION, new ViewVersionAction());
		root.put(Actions.SHOW_TYPE, new ViewTypeAction());
	}

	@Override
	protected void configureShell(Shell shell) {
		shell.setText("Query");
		super.configureShell(shell);
	}

	@Override
	protected MenuManager createMenuManager() {
		this.initActionMap();
		ActionMap am = getContext().getActionMap();

		// file menu
		MenuManager fileManager = new MenuManager("&File");
		fileManager.add(am.get(Actions.OPEN_TEMPLATE));
		fileManager.add(am.get(Actions.OPEN_SESSION));
		// profile menu
		MenuManager profileManager = new MenuManager("&Profile");
		profileManager.add(am.get(Actions.ADD_DRIVER));
		profileManager.add(am.get(Actions.ADD_TEMPLATE));
		profileManager.add(am.get(Actions.SHOW_PROFILE));
		// metadata menu
		MenuManager metaDataManager = new MenuManager("&Meta Data");
		metaDataManager.add(am.get(Actions.SHOW_SCHEMA));
		metaDataManager.add(am.get(Actions.SHOW_VERSION));
		metaDataManager.add(am.get(Actions.SHOW_TYPE));

		MenuManager root = new MenuManager();
		root.add(fileManager);
		root.add(profileManager);
		root.add(metaDataManager);
		return root;
	}

	@Override
	protected Control createContents(Composite parent) {
		SashForm sash = new SashForm(parent, SWT.HORIZONTAL);
		this.createLeft(sash);
		this.createRight(sash);

		SWTUtils.setWeight(sash, 0.25);
		return parent;
	}

	private TabFolder createLeft(SashForm parent) {
		TabFolder folder = new TabFolder(parent, SWT.LEFT);
		Object[][] controls = { { new DriverPane(folder), "Driver" }, { new TemplatePane(folder), "Template" },
				{ new SessionPane(folder), "Session" } };
		for (Object[] pane : controls) {
			TabItem item = new TabItem(folder, SWT.NONE);
			item.setControl((Control) pane[0]);
			item.setText((String) pane[1]);
		}
		folder.setSelection(2);

		WindowUtils.popup(parent, folder);

		return folder;
	}

	private TabFolder createRight(SashForm parent) {
		sqlTabFolder = new TabFolder(parent, SWT.RIGHT);
		return sqlTabFolder;
	}
}
